package com.flower.controller;

import com.flower.pojo.Ditailed;
import com.flower.pojo.Shopping;
import com.flower.service.DitailedService;
import com.flower.service.OrderService;
import com.flower.service.ShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ditailed")
public class DitailedController {
    @Autowired
    private DitailedService ditailedService;
    @Autowired
    private ShoppingService shoppingService;
    @Autowired
    private OrderService orderService;
    @RequestMapping("/addDitailed")
    public Map<String,Object> addDitailed(){
        Map<String,Object> map=new HashMap<>();
        int oid =orderService.getOid();
        List<Shopping> shoppingList=shoppingService.queryAll();
        List<Ditailed> list = new ArrayList<>();
        for (Shopping shopping : shoppingList) {
            Ditailed ditailed = new Ditailed();
            ditailed.setFid(shopping.getFid());
            ditailed.setFnum(shopping.getFnum());
            ditailed.setTotal(shopping.getTotal());
            ditailed.setOid(oid);
            list.add(ditailed);
        }
       if(list.size()>0){
           try {
               int result = ditailedService.addDitailed(list);
               if(result!=-1){
                   map.put("code",200);
                   map.put("msg","结算成功,进入支付页面");
                   map.put("oid",oid);
               }
           } catch (Exception e) {
               map.put("code",201);
               map.put("msg","结算失败,请重试");
           }
       }else {
           map.put("code",202);
           map.put("msg","请添加商品");
       }
        return map;
    }
    @RequestMapping("/queryDitailed/{oid}")
    public Map<String,Object> queryDitailed(@PathVariable("oid") Integer oid){
        Map<String,Object> map=new HashMap<>();
        List<Map<String,Object>> list = ditailedService.queryDitailed(oid);
        if(list.size()>0){
            map.put("code",200);
            map.put("msg","查询成功");
            map.put("data",list);
            Double count=0.0;
            for (Map<String, Object> stringObjectMap : list) {
                count +=Double.parseDouble(stringObjectMap.get("total").toString());
            }
            map.put("count",count);
        }else{
            map.put("code",201);
            map.put("msg","暂无数据");
        }
        return map;
    }
}
